package com.mcflies.ui.menu;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.mcflies.ui.R;

/**
 * @author:mcfly_jun
 * @create_date:2012-8-20
 * @comment:自定义样式的Menu
 */
public class AndroidCustomMenuStyleActivity extends Activity {
	private boolean isMore = true;// Menu菜单翻页控制
	private AlertDialog menuDialog;// Menu菜单Dialog
	private GridView customMenuStyleMenu_GV;
	private View menuView;

	private String[] firstPageMenuTitleArray;// 第一页菜单对应的菜单标题数组
	private int[] firstPageMenuImageArray;// 第一页菜单对应的菜单图标数组
	private String[] secondPageMenuTitleArray;// 第二页菜单对应的菜单标题数组
	private int[] secondPageMenuImageArray;// 第二页菜单对应的菜单图标数组

	private final int ITEM_MORE = 11;// 菜单

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add("menu");// 必须创建一项
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		if (null == menuDialog) {
			menuDialog = new AlertDialog.Builder(this).setView(menuView).show();
		} else {
			menuDialog.show();
		}
		return false;// 返回为true 则显示系统menu
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.android_menu_introduce_main);
		initData();
		initWidget();
		registWidgetEvent();
	}

	private void registWidgetEvent() {
		// 对弹出框设置按键监听时间
		menuDialog.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				switch (keyCode) {
				case KeyEvent.KEYCODE_MENU:
					menuDialog.dismiss();
					break;
				}
				return false;
			}
		});

		// 为GridView项注册点击监听事件
		customMenuStyleMenu_GV
				.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> adapter, View view,
							int position, long id) {
						switch (position) {
						case ITEM_MORE:
							if (isMore) {
								customMenuStyleMenu_GV
										.setAdapter(getMenuAdapter(
												secondPageMenuTitleArray,
												secondPageMenuImageArray));
								isMore = false;
							} else {
								customMenuStyleMenu_GV
										.setAdapter(getMenuAdapter(
												firstPageMenuTitleArray,
												firstPageMenuImageArray));
								isMore = true;
							}
							customMenuStyleMenu_GV.invalidate();// 更新menu
							customMenuStyleMenu_GV.setSelection(ITEM_MORE);
							break;
						}
					}
				});

	}

	private void initWidget() {
		menuView = View.inflate(this, R.layout.android_custom_menu_style_menu,
				null);

		menuDialog = new AlertDialog.Builder(this).create();
		menuDialog.setView(menuView);

		customMenuStyleMenu_GV = (GridView) menuView
				.findViewById(R.id.customMenuStyleMenu_GV);
		// 为GridView添加适配器
		customMenuStyleMenu_GV.setAdapter(getMenuAdapter(
				firstPageMenuTitleArray, firstPageMenuImageArray));
	}

	private void initData() {
		firstPageMenuTitleArray = new String[] { "搜索", "文件管理", "下载管理", "全屏",
				"网址", "书签", "加入书签", "分享页面", "退出", "夜间模式", "刷新", "更多" };
		firstPageMenuImageArray = new int[] { R.drawable.menu_search,
				R.drawable.menu_filemanager, R.drawable.menu_downmanager,
				R.drawable.menu_fullscreen, R.drawable.menu_inputurl,
				R.drawable.menu_bookmark, R.drawable.menu_bookmark_sync_import,
				R.drawable.menu_sharepage, R.drawable.menu_quit,
				R.drawable.menu_nightmode, R.drawable.menu_refresh,
				R.drawable.menu_more };

		secondPageMenuTitleArray = new String[] { "自动横屏", "笔选模式", "阅读模式",
				"浏览模式", "快捷翻页", "检查更新", "检查网络", "定时刷新", "设置", "帮助", "关于", "返回" };
		secondPageMenuImageArray = new int[] { R.drawable.menu_auto_landscape,
				R.drawable.menu_penselectmodel, R.drawable.menu_page_attr,
				R.drawable.menu_novel_mode, R.drawable.menu_page_updown,
				R.drawable.menu_checkupdate, R.drawable.menu_checknet,
				R.drawable.menu_refreshtimer, R.drawable.menu_syssettings,
				R.drawable.menu_help, R.drawable.menu_about,
				R.drawable.menu_return };
	}

	/**
	 * @param menuNameArray
	 * @param imageResourceArray
	 * @return
	 * @commont:封装GridView适配器
	 */
	private SimpleAdapter getMenuAdapter(String[] menuNameArray,
			int[] imageResourceArray) {
		ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < menuNameArray.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("itemImage", imageResourceArray[i]);
			map.put("itemText", menuNameArray[i]);
			data.add(map);
		}

		SimpleAdapter simperAdapter = new SimpleAdapter(this, data,
				R.layout.android_custom_menu_style_gridview, new String[] {
						"itemImage", "itemText" }, new int[] {
						R.id.customMenuStyleGridView_IV,
						R.id.customMenuStyleGridView_TV });
		return simperAdapter;
	}
}
